package com.lovo.work.dao.impl;

import com.lovo.common.dao.BaseDao;
import com.lovo.common.model.CutPageBean;
import com.lovo.common.model.EmpBean;
import com.lovo.common.model.WorkBean;
import com.lovo.work.dao.IWorkDao;

import java.sql.SQLException;
import java.util.List;
/**
 * 职位管理Dao实现类
 * @author 白德柯
 *
 */
public class WorkDaoImpl extends BaseDao implements IWorkDao {
    @Override
    public CutPageBean<WorkBean> findByItem(int page) {
        {
            String sql = "SELECT w.*\n" +
                    ",sys_data.dict_value data_perm_value\n" +
                    ",sys_school.dict_value school_perm_value\n" +
                    ",sys_user.dict_value user_perm_value\n" +
                    ",sys_total.dict_value total_perm_value\n" +
                    "FROM work_info w\n" +
                    "JOIN sys_dict sys_data ON w.data_perm=sys_data.id\n" +
                    "JOIN sys_dict sys_school ON w.school_perm=sys_school.id\n" +
                    "JOIN sys_dict sys_user ON w.user_perm=sys_user.id\n" +
                    "JOIN sys_dict sys_total ON w.total_perm=sys_total.id";

            CutPageBean<WorkBean> cutPageBean = new CutPageBean<>();
            this.setConnection();
            try {
                ps = con.prepareStatement(sql + CutPageBean.getPageSql(page));
                rs = ps.executeQuery();
                while(rs.next()){
                    cutPageBean.getList().add(fill());
                }
                ps = con.prepareStatement("SELECT COUNT(*) cnt FROM ("+ sql +") t");
                rs = ps.executeQuery();
                rs.next();
                // 获取员工总数并设置给分页对象
                cutPageBean.setCount(rs.getInt("cnt"));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                this.close();
            }
            return cutPageBean;
        }
    }
    private WorkBean fill() throws SQLException {
        WorkBean workBean = new WorkBean();
        workBean.setId(rs.getInt("id"));
        workBean.setWorkName(rs.getString("work_name"));
        workBean.setInfo(rs.getString("info"));
        workBean.setDataPerm(rs.getInt("data_perm"));
        workBean.setSchoolPerm(rs.getInt("school_perm"));
        workBean.setUserPerm(rs.getInt("user_perm"));
        workBean.setTotalPerm(rs.getInt("total_perm"));
        workBean.setDataPermValue(rs.getString("data_perm_value"));
        workBean.setSchoolPermValue(rs.getString("school_perm_value"));
        workBean.setUserPermValue(rs.getString("user_perm_value"));
        workBean.setTotalPermValue(rs.getString("total_perm_value"));
        return workBean;
    }

    @Override
    public List<WorkBean> findAll() {
        return null;
    }

    public static void main(String[] args) {
        IWorkDao workDao = new WorkDaoImpl();
        System.out.println(workDao.findByItem(1));
    }
}
